﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Cms.Model;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;

namespace Portal.Cms.Bussiness
{
    public class GroupDataFolderProcessBll
    {
        public static GroupDataFolderCls CreateGroupDataFolderModel(string SiteId, string GroupDataFolderId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableGroupDataFolder " +
                                               "     where GroupDataFolderId=@GroupDataFolderId order by SortIndex",
                    new DbParam[] 
                    { 
                        new DbParam("GroupDataFolderId", GroupDataFolderId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            GroupDataFolderCls OGroupDataFolder = GroupDataFolderParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return OGroupDataFolder;
        }



        public static GroupDataFolderCls[] ReadingGroupDataFolder(string SiteId, string GroupDataId, string ParentGroupDataFolderId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query="select * from TableGroupDataFolder where frkGroupDataId='" + GroupDataId + "' ";
            if (!string.IsNullOrEmpty(ParentGroupDataFolderId))
            {
                Query += " and frkParentGroupDataFolderId='" + ParentGroupDataFolderId + "'";
            }
            else 
            {
                Query += " and frkParentGroupDataFolderId is null";
            }
            Query += " order by SortIndex";
            DataSet dsResult = DBService.GetDataSet(null, Query, null);
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new GroupDataFolderCls[0];
            }
            GroupDataFolderCls[] GroupDataFolder = GroupDataFolderParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return GroupDataFolder;
        }


        public static void AddGroupDataFolder(
            string SiteId,
            string GroupDataId,
            string FolderName,
            string Description,
            string ParentGroupDataFolderId,
            string SortIndex)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string GroupDataFolderId = System.Guid.NewGuid().ToString();
            string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            if (string.IsNullOrEmpty(ParentGroupDataFolderId)) ParentGroupDataFolderId = null;
            DBService.Insert(null, "TableGroupDataFolder",
                    new DbParam[] 
                    { 
                        new DbParam("GroupDataFolderId", GroupDataFolderId),
                        new DbParam("frkGroupDataId", GroupDataId),
                        new DbParam("FolderName", FolderName),
                        new DbParam("Description", Description),
                        new DbParam("frkParentGroupDataFolderId", ParentGroupDataFolderId),
                        new DbParam("SortIndex", SortIndex),
                    });
        }


        public static void SaveGroupDataFolder(string SiteId, string GroupDataFolderId, string FolderName, string Description, string SortIndex)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DBService.Update(null, "TableGroupDataFolder", "GroupDataFolderId", GroupDataFolderId,
                    new DbParam[] 
                    { 
                        new DbParam("FolderName", FolderName),
                        new DbParam("Description", Description),
                    });
        }


        public static void DeleteGroupDataFolder(string SiteId, string GroupDataFolderId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query =
                " Delete from TableDataBelongGroupDataFolder where frkGroupDataFolderId=@GroupDataFolderId; "+
                " Delete from TableGroupDataFolder where GroupDataFolderId=@GroupDataFolderId; ";
            DBService.ExecuteNonQuery(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("GroupDataFolderId", GroupDataFolderId),
                    });
        }
    }
}
